﻿using System;
using System.Collections.Generic;
using System.Text;

namespace EChartsApp
{
    public static partial class Ext
    {
        /// <summary>
        /// 格式化对象
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="ignoreDefaultValue">属性值与默认属性值相同，是否忽略</param>
        /// <returns></returns>
        public static string ToEChartJson(this object obj, bool ignoreDefaultValue = true, bool ignoreNullValue = true)
        {
            if (obj == null) return string.Empty;
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("{");
            var type = obj.GetType();
            var props = type.GetProperties();
            object initObj = null;
            if (ignoreDefaultValue) initObj = Activator.CreateInstance(type);
            foreach (var prop in props)
            {
                var value = prop.GetValue(obj, null);
                if (value == null)
                {
                    if (!ignoreNullValue)
                    {
                        strBuilder.Append($"{prop.Name}:\"\",");
                    }
                    continue;
                }

                if (ignoreDefaultValue)
                {
                    var initValue = prop.GetValue(initObj, null);
                    if (value.Equals(initValue))
                    {
                        continue;
                    }

                    if (value is List<object> lst1 && initValue is List<object> lst2)
                    {
                        if (string.Join(",", lst1).Equals(string.Join(",", lst2)))
                        {
                            continue;
                        }
                    }
                }

                var valueType = value.GetType();
                if (valueType.IsPrimitive)
                {
                    if (bool.TryParse(value.ToString(), out bool blValue))
                    {
                        strBuilder.Append($"{prop.Name}:{(blValue ? "true" : "false")},");
                    }
                    else
                    {
                        strBuilder.Append($"{prop.Name}:{value},");
                    }
                }
                else if (value is string || valueType.IsEnum)
                {
                    strBuilder.Append($"{prop.Name}:\"{value}\",");
                }
                else if (value is List<object> objs)
                {
                    if (objs.Count <= 0)
                    {
                        continue;
                    }
                    if (objs[0] is string)
                    {
                        value = "\"" + string.Join("\",\"", objs) + "\"";
                    }
                    else if (objs[0].GetType().Namespace.StartsWith("WUDI.ECharts.Series."))
                    {
                        var lstItemStr = new List<string>();
                        foreach (var item in objs)
                        {
                            lstItemStr.Add(item.ToEChartJson());
                        }
                        value = string.Join(",", lstItemStr);
                    }
                    else
                    {
                        value = string.Join(",", objs);
                    }
                    strBuilder.Append($"{prop.Name}:[{value}],");
                }
                else
                {
                    if (value.ToEChartJson().Equals("{}"))
                    {
                        continue;
                    }
                    strBuilder.Append($"{prop.Name}:{value.ToEChartJson()},");
                }
            }
            var result = strBuilder.ToString().Trim(',') + "}";
            return result;
        }
    }
}
